package com.work.libtest2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.work.libtest2.dto.EmployeeDto;
import com.work.libtest2.entity.Department;
import com.work.libtest2.entity.Employee;
import com.work.libtest2.entity.Job;
import com.work.libtest2.mapper.DepartmentMapper;
import com.work.libtest2.mapper.EmployeeMapper;
import com.work.libtest2.mapper.JobMapper;
import com.work.libtest2.service.EmployeeService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * ClassName: EmployeeServiceImpl
 * Package: com.work.libtest2.service
 * Description:
 *
 * @Author
 * @Create 2024/6/5 21:32
 * @Version 1.0
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper,Employee> implements EmployeeService{
    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private JobMapper jobMapper;
    @Resource
    private DepartmentMapper deptMapper;

    /**
     *  根据员工的id查询员工
     * */
    @Override
    public Employee getEmployeeById(int i) {
        return employeeMapper.selectById(i);
    }

    @Override
    public IPage<EmployeeDto> getPage(int currentPage, int pageSize, Employee employee) {
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(employee.getEmpName()),Employee::getEmpName,employee.getEmpName());
        lqw.like(Strings.isNotEmpty(employee.getAddress()),Employee::getAddress,employee.getAddress());
        lqw.like(Strings.isNotEmpty(employee.getGender()),Employee::getGender,employee.getGender());
        lqw.like(employee.getDeptId()!=null,Employee::getDeptId,employee.getDeptId());
        lqw.like(employee.getJobId()!=null,Employee::getJobId,employee.getJobId());
        IPage page = new Page(currentPage,pageSize);
        employeeMapper.selectPage(page,lqw);
//        将工种的名字和部门名字加入到employeeDto中
        IPage<EmployeeDto> newPage = new Page<>();

        BeanUtils.copyProperties(page,newPage);
        List<EmployeeDto> list = new ArrayList<>();
        newPage.setRecords(list);

        for (int i = 0; i < page.getRecords().size(); i++) {
            EmployeeDto employeeDto = new EmployeeDto();
            BeanUtils.copyProperties(page.getRecords().get(i),employeeDto);
//            获取部门名字
            Job job = jobMapper.selectById(employeeDto.getJobId());
            employeeDto.setJobName(job.getJobName());
//            获取工种名字
            Department department = deptMapper.selectById(employeeDto.getDeptId());
            employeeDto.setDeptName(department.getDeptName());
            list.add(employeeDto);
        }
        return newPage;
    }

    @Override
    public int deleteEmpById(Integer empId) {
        return employeeMapper.deleteById(empId);
    }

    @Override
    public int saveEmployee(Employee employee) {
        return employeeMapper.insert(employee);
    }

    @Override
    public int updateEmployeeById(Employee employee) {
        return employeeMapper.updateById(employee);
    }

    @Override
    public List<Employee> getAllEmpName() {
        return employeeMapper.selectList(null);
    }
}
